<template>
  <div>
    <el-form :model="dataInfo" :rules="rules" ref="dataInfo" >
      <div class="detail-info" style="margin-top: 20px">
        <div class="form-item">
          <h4>公告信息</h4>
          <el-form-item label="标题" prop="title">
            <el-input v-model="dataInfo.title" style="width:220px"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input type="textarea" v-model="dataInfo.content" style="width:220px"></el-input>
          </el-form-item>
          <el-form-item label="ZIP包" :error="zipError" required>
            <el-upload
              class="upload-demo"
              ref="upload"
              name="zipFile"
              style="width:350px"
              :action="url"
              :before-upload="handleBeforeUpload"
              :on-remove="handleRemove"
              :on-success="onSuccess"
              :on-error="onError"
              :file-list="fileList"
              :auto-upload="true"
              accept=".zip">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传<span style="color:red"> 1 </span>个<span style="color:red"> zip </span>文件，且不超过<span style="color:red"> 90MB </span></div>
            </el-upload>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <div style="margin: 20px auto 20px;width:20%">
      <el-button @click="onSubmit('dataInfo')" type="success">提交</el-button>
      <el-button @click="cancel(menuId, 1)" type="danger">取消</el-button>
    </div>
  </div>
</template>
<script>
  import { apiHost, apiURL } from '../../../config/settings.js'
  import { gzt } from '../../../config/proxy.js'
  import mixins from '../../../components/mixins.vue'
  export default {
    mixins: [mixins],
    data () {
      return {
        dataInfo: {
          title: '', // 标题
          content: '', // 内容
          zipUrl: '' // url
        },
        zipError: '', // zip的错误信息
        fileList: [], // 上传文件列表
        zipFlag: false, // 上传的状态
        url: '', // 上传地址
        menuId: '', // 打开的菜单id
        rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur, change' },
            { min: 1, max: 20, message: '标题最大长度为20个字符', trigger: 'blur, change' }
          ],
          content: [
            { required: true, message: '请输入内容', trigger: 'blur, change' },
            { min: 1, max: 5000, message: '标题最大长度为5000个字符', trigger: 'blur, change' }
          ]
        }
      }
    },
    methods: {
      submitUpload () {
        this.$refs.upload.submit()
      },
      handleRemove (file, fileList) {
        console.log(file)
        console.log(fileList)
        console.log(this.fileList)
        if (fileList.length === 0) {
          this.zipError = '请上传zip包'
          this.data.zipUrl = ''
          this.zipFlag = false
        }
      },
      handleBeforeUpload (file) {
        console.log(file)
        this.fileList = []
        this.fileList.push(file)
      },
      onSuccess (res, file, fileList) {
        console.log('res', res)
        if (res.message === 'ok') {
          this.dataInfo.zipUrl = res.content.zipUrl
          this.message('上传zip包成功', 'success')
          this.zipError = ''
          this.zipFlag = true
        } else {
          this.message(res.message, 'error')
          this.fileList = []
          this.zipError = '请上传zip包'
          this.zipFlag = false
        }
      },
      onError (error) {
        console.log(error)
      },
      handleExceed (files, fileList) {
        console.log(arguments)
      },
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (!this.zipFlag) {
            this.zipError = '请上传zip包'
          } else {
            this.zipError = ''
          }
          if (valid && this.zipFlag) {
            var _data = Object.assign({}, this.dataInfo)
            _data.branchId = this.branchId
            _data.createUser = this.userId
            _data.createUserName = this.userName
            _data.departmentId = this.deptId
            console.log(_data)
            gzt.gonggaogl.add(_data, res => {
              console.log(res)
              if (res.message === 'ok') {
                this.message('创建成功', 'success')
                this.cancel(this.menuId)
                this.$store.dispatch('gonggaoglState', this.$store.state.updateState.gonggaoglState + 1)
              } else {
                this.message(res.message, 'error')
              }
            })
          } else {
            console.log('bbb')
            return false
          }
        })
      },
      cancel (id, num) { // 取消
        if (num) {
          this.$confirm('确定要取消？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$store.dispatch('delTab', {id: id})
          }).catch(() => {
          })
        } else {
          this.$store.dispatch('delTab', {id: id})
        }
      },
      message (text, type) { // 消息提示
        this.$message({
          showClose: true,
          message: text,
          type: type
        })
      }
    },
    computed: {
      update_state: function () {
        // console.log(state.updateState.dhmState)
        return this.$store.state.updateState.pttcState
      },
      userName: function () {
        if (!this.$store.state.user.info.user_name) {
          this.$store.dispatch('SET_TOKEN')
        }
        return this.$store.state.user.info.user_name
      },
      userId: function () {
        return this.$store.state.user.info.user_id
      },
      roleId: function () {
        return this.$store.state.user.info.roleid
      },
      branchName: function () {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function () {
        return this.$store.state.user.info.branchId
      },
      deptName: function () {
        return this.$store.state.user.info.parOrgName
      },
      deptId: function () {
        return this.$store.state.user.info.or_org_id
      },
      branchOrgId: function () {
        return this.$store.state.user.info.branchOrgId
      },
      uorId: function () {
        return this.$store.state.user.info.uor_id
      },
      user_telphone: function () {
        return this.$store.state.user.info.user_telphone
      }
    },
    created () {
      this.url = 'http://' + apiHost.newCms + ':20000' + apiURL.upLoadZip
      this.menuId = this.$store.state.tab.menuId
      console.log(this.url)
    }
  }
</script>
